package StudentProject.jdbc;

import java.sql.*;

/**
 * 直接使用jdbc连接的测试
 */
public class Jdbc {
    public static void main(String[] args) {
        //加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("驱动加载成功");
            //连接数据库
            try {
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project", "root", "root");

                //创建执行环境
                Statement statement = conn.createStatement();
                //执行sql语句，得到结果集
                ResultSet resultSet = statement.executeQuery("select * from user");
                //循环指针获取对应信息
                while (resultSet.next()){
                    System.out.println("账号:" + resultSet.getInt("userid") + " ");
                    System.out.println("密码:" + resultSet.getString("password"));
                }


            } catch (SQLException throwables) {
                throwables.printStackTrace();
                System.out.println("数据连接失败");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("驱动加载失败");
        }
    }
}
